package com.uuah.server.context;

import javax.resource.spi.work.WorkListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.uuah.api.config.IThreadingProfile;
import com.uuah.api.work.IWorkManager;
import com.uuah.server.UuahContext;
import com.uuah.server.api.IUuahContext;
import com.uuah.server.api.config.IUuahConfiguration;
import com.uuah.server.api.context.IUuahContextBuilder;
import com.uuah.server.api.context.notification.linstener.IAgentMBeanNotificationListener;
import com.uuah.server.api.context.notification.linstener.IConnectionNotificationListener;
import com.uuah.server.api.context.notification.linstener.IExceptionNotificationListener;
import com.uuah.server.api.context.notification.linstener.IServiceNotificationListener;
import com.uuah.server.api.context.notification.linstener.IUuahContextNotificationListener;
import com.uuah.server.api.lifecycle.ILifecycleManager;
import com.uuah.server.config.UuahConfiguration;
import com.uuah.server.context.notification.AgentMBeanNotification;
import com.uuah.server.context.notification.ConnectionNotification;
import com.uuah.server.context.notification.ExceptionNotification;
import com.uuah.server.context.notification.ServerNotificationManager;
import com.uuah.server.context.notification.ServiceNotification;
import com.uuah.server.context.notification.UuahContextNotification;
import com.uuah.server.lifecycle.GenericLifecycleManager;
import com.uuah.server.lifecycle.phases.UuahContextDisposePhase;
import com.uuah.server.lifecycle.phases.UuahContextInitialisePhase;
import com.uuah.server.lifecycle.phases.UuahContextStartPhase;
import com.uuah.server.lifecycle.phases.UuahContextStopPhase;
import com.uuah.utils.ClassUtils;
import com.uuah.work.UuahWorkListener;
import com.uuah.work.UuahWorkManager;

/**
 * Implementation of {@link IUuahContextBuilder} that uses {@link UuahContext} as
 * the default {@link IUuahContext} implementation and builds it with defaults
 * values for {@link IUuahConfiguration}, {@link ILifecycleManager},
 * {@link IWorkManager}, {@link WorkListener} and
 * {@link ServerNotificationManager}.
 *
 * <p>
 * update record:updator，update time，update content and version
 * </p>
 *
 * @author <a href="jonny_quan@hotmail.com">jonny</a>
 * @date 2009 2009-3-27 下午01:34:55
 * @version 1.0.0
 */
public class UuahContextBuilder implements IUuahContextBuilder {
	protected transient final Logger logger = LoggerFactory
			.getLogger(UuahContextBuilder.class);

	protected IUuahConfiguration config;

	protected ILifecycleManager lifecycleManager;

	protected IWorkManager workManager;

	protected WorkListener workListener;

	protected ServerNotificationManager notificationManager;

	public IUuahContext buildUuahContext() {
		if (logger.isDebugEnabled()) {
			logger
					.debug("Building new DefaultUuahContext instance with UuahContextBuilder: "
							+ this);
		}
		IUuahContext uuahContext = new UuahContext(getUuahConfiguration(),
				getWorkManager(), getWorkListener(), getLifecycleManager(),
				getNotificationManager());
		return uuahContext;
	}

	public void setUuahConfiguration(IUuahConfiguration config) {
		this.config = config;
	}

	public void setWorkManager(IWorkManager workManager) {
		this.workManager = workManager;
	}

	public void setWorkListener(WorkListener workListener) {
		this.workListener = workListener;
	}

	public void setNotificationManager(
			ServerNotificationManager notificationManager) {
		this.notificationManager = notificationManager;
	}

	public void setLifecycleManager(ILifecycleManager lifecycleManager) {
		this.lifecycleManager = lifecycleManager;
	}

	protected IUuahConfiguration getUuahConfiguration() {
		if (config != null) {
			return config;
		} else {
			return new UuahConfiguration();
		}
	}

	protected ILifecycleManager getLifecycleManager() {
		if (lifecycleManager != null) {
			return lifecycleManager;
		} else {
			ILifecycleManager lifecycleManager = new GenericLifecycleManager();
			lifecycleManager.registerLifecycle(new UuahContextInitialisePhase());
			lifecycleManager.registerLifecycle(new UuahContextStartPhase());
			lifecycleManager.registerLifecycle(new UuahContextStopPhase());
			lifecycleManager.registerLifecycle(new UuahContextDisposePhase());
			return lifecycleManager;
		}
	}

	protected IWorkManager getWorkManager() {
		if (workManager != null) {
			return workManager;
		} else {
			return new UuahWorkManager(
					IThreadingProfile.DEFAULT_THREADING_PROFILE, "UuahAppServer");
		}
	}

	protected WorkListener getWorkListener() {
		if (workListener != null) {
			return workListener;
		} else {
			return new UuahWorkListener();
		}
	}

	protected ServerNotificationManager getNotificationManager() {
		if (notificationManager != null) {
			return notificationManager;
		} else {
			ServerNotificationManager notificationManager = new ServerNotificationManager();
			notificationManager.addInterfaceToType(
					IUuahContextNotificationListener.class,
					UuahContextNotification.class);
			notificationManager.addInterfaceToType(
					IServiceNotificationListener.class,
					ServiceNotification.class);
			notificationManager.addInterfaceToType(
					IConnectionNotificationListener.class,
					ConnectionNotification.class);
			notificationManager.addInterfaceToType(
					IExceptionNotificationListener.class,
					ExceptionNotification.class);
			notificationManager.addInterfaceToType(
					IAgentMBeanNotificationListener.class,
					AgentMBeanNotification.class);
			return notificationManager;
		}
	}

	public String toString() {
		return ClassUtils.getClassName(getClass()) + "{uuahConfiguration="
				+ config + ", lifecycleManager=" + lifecycleManager
				+ ", workManager=" + workManager + ", workListener="
				+ workListener + ", notificationManager=" + notificationManager
				+ "}";
	}
}
